/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.playdialog.PrePlayMatchDialog;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.GameManager;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author Massimo Canonaco
 *
 */
public class GameManagerPanel extends JPanel
{
	/** */
	static public final String GAME_MANAGER_PANEL_NAME = "gameManagerPanel";
	
	/** */
	static private GridBagConstraints constraints;
	
	/** */
	private GameManager gameManager;
	
	/** */
	private JLabel informations;
	
	/** */
	private GameManagerMenu gameManagerMenu;
	
	/** */
	private GridBagLayout layoutManager;
	
	/** */
	private Image background;
	
	public GameManagerPanel(GameManagerMenu gmm, Map settingsMap)
	{
		super();
		layoutManager = new GridBagLayout();
		setLayout(layoutManager);
		this.gameManagerMenu = gmm;
		OverallGUILayoutFactory factory = OverallGUILayoutFactory.getInstance();
		
		//funzione che imposta i vincoli per il layout manager goes here
		final String 
				TEXT_INACTIVE_SIZE = "gamemenu.text.inactive.size",
				TEXT_INACTIVE_COLOR = "gamemenu.text.inactive.color",
				TEXT_ACTIVE_SIZE = "gamemenu.text.active.size",
				TEXT_ACTIVE_COLOR = "gamemenu.text.active.color",
				STROKE_COLOR = "gamemenu.text.stroke.color",
				STROKE_SIZE = "gamemenu.text.stroke",
				STORE_WIDTH = "gamemenu.store.button.width",
				STORE_HEIGHT = "gamemenu.store.button.height",
				STORE_ICON = "gamemenu.store.button.path",
				SAVE_WIDTH = "gamemenu.save.button.width",
				SAVE_HEIGHT = "gamemenu.save.button.height",
				SAVE_ICON = "gamemenu.save.button.path",
				PLAY_WIDTH = "gamemenu.play.button.width",
				PLAY_HEIGHT = "gamemenu.play.button.height",
				PLAY_ICON = "gamemenu.play.button.path",
				EXIT_WIDTH = "gamemenu.exit.button.width",
				EXIT_HEIGHT = "gamemenu.exit.button.height",
				EXIT_ICON = "gamemenu.exit.button.path",
				COMPONENT_SPACING = "gamemenu.component.spacing",
				INFORMATION_SIZE = "gamemenu.text.information.size",
				INFORMATION_BACKGROUND = "gamemenu.text.information.path",
				INFORMATION_PANEL_HEIGHT = "gamemenu.information.height";
		
		gameManager = gameManagerMenu.getMainFrame().getGameManager();
		int componentSpacing = Integer.parseInt((String)settingsMap.get(COMPONENT_SPACING)),
				informationsHeight = Integer.parseInt((String)settingsMap.get(INFORMATION_PANEL_HEIGHT));
		if(constraints == null)
		 {
			 constraints = new GridBagConstraints();
			 
			 constraints.fill = GridBagConstraints.NONE;
			 constraints.weightx = 0;
			 constraints.weighty = 0;
			 constraints.gridy = 0;
			 constraints.insets = new Insets(componentSpacing, 0, componentSpacing, 0);
		 }
		
		
		float activeSize = Float.parseFloat((String)settingsMap.get(TEXT_ACTIVE_SIZE)), 
				inactiveSize = Float.parseFloat((String)settingsMap.get(TEXT_INACTIVE_SIZE)),
				strokeSize = Float.parseFloat((String)settingsMap.get(STROKE_SIZE)),
				informationSize = Float.parseFloat((String)settingsMap.get(INFORMATION_SIZE));
		Color active = factory.getParsedColor((String)settingsMap.get(TEXT_ACTIVE_COLOR)), 
				inactive = factory.getParsedColor((String)settingsMap.get(TEXT_INACTIVE_COLOR)),
				strokecolor = factory.getParsedColor((String)settingsMap.get(STROKE_COLOR));
		
//		background = Toolkit.getDefaultToolkit().createImage((String)settingsMap.get(INFORMATION_BACKGROUND));

		try {
			background = ImageIO.read(new File((String)settingsMap.get(INFORMATION_BACKGROUND)));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		setOpaque(false);
		
		JButton storeButton = this.getButton(factory, Integer.parseInt((String)settingsMap.get(STORE_WIDTH)), Integer.parseInt((String)settingsMap.get(STORE_HEIGHT)), "Negozio", (String)settingsMap.get(STORE_ICON), active, inactive, activeSize, inactiveSize, strokecolor, strokeSize),
				saveButton = this.getButton(factory, Integer.parseInt((String)settingsMap.get(SAVE_WIDTH)), Integer.parseInt((String)settingsMap.get(SAVE_HEIGHT)), "Salva", (String)settingsMap.get(SAVE_ICON), active, inactive, activeSize, inactiveSize, strokecolor, strokeSize),
				playButton = this.getButton(factory, Integer.parseInt((String)settingsMap.get(PLAY_WIDTH)), Integer.parseInt((String)settingsMap.get(PLAY_HEIGHT)), "Gioca", (String)settingsMap.get(PLAY_ICON), active, inactive, activeSize, inactiveSize, strokecolor, strokeSize),
				exitButton = this.getButton(factory, Integer.parseInt((String)settingsMap.get(EXIT_WIDTH)), Integer.parseInt((String)settingsMap.get(EXIT_HEIGHT)), "Esci", (String)settingsMap.get(EXIT_ICON), active, inactive, activeSize, inactiveSize, strokecolor, strokeSize);
		
		int maxRow = storeButton.getWidth() + saveButton.getWidth();
		
		if(maxRow < playButton.getWidth()+exitButton.getWidth())
		{
			maxRow = playButton.getWidth()+exitButton.getWidth();
		}
		maxRow += componentSpacing;
		
		JPanel infoPanel = new JPanel()
		{
			@Override
			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				g.drawImage(background, 0, 0, this.getWidth(), this.getHeight(), null);
			}
		};
		Dimension infoPanelDimension = new Dimension(gameManagerMenu.getMainFrame().getWidth(), informationsHeight);
		infoPanel.setSize(infoPanelDimension);
		infoPanel.setPreferredSize(infoPanelDimension);
		infoPanel.setMinimumSize(infoPanelDimension);
		informations = new JLabel();
		informations.setFont(factory.getHeaderFont().deriveFont(informationSize));
		informations.setForeground(inactive);
		informations.setOpaque(false);
		infoPanel.add(informations);
		add(infoPanel,constraints);
		
		constraints.gridy = 1;
		constraints.gridx = 0;
		JPanel firstRow = new JPanel();
		BoxLayout b = new BoxLayout(firstRow, BoxLayout.X_AXIS);
		Dimension firstRowDimension = new Dimension(maxRow, storeButton.getHeight());
		firstRow.setPreferredSize(firstRowDimension);
		firstRow.setSize(firstRowDimension);
		firstRow.setMinimumSize(firstRowDimension);
		firstRow.setLayout(b);
		firstRow.add(storeButton);
		firstRow.setOpaque(false);
		firstRow.add(Box.createHorizontalGlue());
		firstRow.add(saveButton);
		firstRow.setOpaque(false);
		add(firstRow,constraints);
		
		constraints.gridy = 2;
		Dimension secondRowDimension = new Dimension(maxRow, exitButton.getHeight());
		JPanel secondRow = new JPanel();
		secondRow.setOpaque(false);
		secondRow.setLayout(new BoxLayout(secondRow,BoxLayout.X_AXIS));
		secondRow.setSize(secondRowDimension);
		secondRow.setPreferredSize(secondRowDimension);
		secondRow.setMinimumSize(secondRowDimension);
		secondRow.add(playButton);
		secondRow.add(Box.createHorizontalGlue());
		secondRow.add(exitButton);
		secondRow.setOpaque(false);
		add(secondRow,constraints);
		
		ShowInformations si = new ShowInformations();
		playButton.setName(" Gioca un incontro ");
		storeButton.setName(" Accedi al negozio ");
		saveButton.setName(" Salva i tuoi dati ");
		exitButton.setName(" Ritorna al menu iniziale ");
		
		storeButton.addMouseMotionListener(si);
		storeButton.addFocusListener(si);
		saveButton.addMouseMotionListener(si);
		saveButton.addFocusListener(si);
		playButton.addMouseMotionListener(si);
		playButton.addFocusListener(si);
		playButton.addMouseMotionListener(si);
		playButton.addFocusListener(si);
		exitButton.addMouseMotionListener(si);
		exitButton.addFocusListener(si);
		
		storeButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				gameManagerMenu.showStorePanel();
				
			}
		});
		
		playButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				PrePlayMatchDialog ppmDialog = new PrePlayMatchDialog(gameManagerMenu);
				ppmDialog.show();
//				gameManagerMenu.showSMP();
			}
		});
		exitButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
//				try {
					gameManager.reset();
					gameManagerMenu.getMainFrame().getToMain();
//				} catch (IOException | FontFormatException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
			}
		});
		
		saveButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				gameManager.getProfileLoader().save(gameManager.getClientPlayer(),true);
			}
		});
	}
	
	private JButton getButton(OverallGUILayoutFactory factory, int width, int height, String text, String image, Color active, Color inactive, 
							   float activeSize, float inactiveSize, Color strokecolor, float strokeSize)
	{
		Font font = factory.getHeaderFont();
		Dimension buttonSize = new Dimension(width, height);
		ImageIcon activeIcon = new ImageIcon(factory.createImageButton(activeSize, text, active, 
				strokeSize, strokecolor,
				width, height, image, font));
		
		ImageIcon inactiveIcon = new ImageIcon(factory.createImageButton(inactiveSize, text, inactive, 
				strokeSize, strokecolor, width, height, image,font));
		JButton button = new JButton(inactiveIcon);
		button.setRolloverIcon(activeIcon);
		button.setSelectedIcon(activeIcon);
		button.setDisabledSelectedIcon(inactiveIcon);
		button.setBorderPainted(false);
		button.setPreferredSize(buttonSize);
		button.setMinimumSize(buttonSize);
		button.setSize(buttonSize);
		return button;
		
	}
	
	/** */
	private class ShowInformations implements FocusListener, MouseMotionListener
	{

		@Override
		public void mouseDragged(MouseEvent arg0) {
			
			
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			((Component)e.getSource()).requestFocusInWindow();
			
		}

		@Override
		public void focusGained(FocusEvent e) {
			informations.setText(((Component)e.getSource()).getName());
		}

		@Override
		public void focusLost(FocusEvent arg0) {
		}
		
	}
}
